//
//  STImageSkimController.m
//  demo
//
//  Created by 戈王 on 15/4/28.
//  Copyright (c) 2015年 nice. All rights reserved.
//

#import "STImageSkimController.h"
#import "UIImageView+WebCache.h"
#import "UIView+Extension.h"
#import "VIPhotoView.h"
#import "UIImage+iOS6and7.h"
#import "NSArray+STRExtention.h"

typedef enum
{
    kImageSkimTypeStart = 1,
    kImageSkimTypeProgress,
    kImageSkimTypeFinal,
    kImageSkimTypeStartAndFinal
} kImageSkimType;

@interface STImageSkimController ()<UIScrollViewDelegate ,UIGestureRecognizerDelegate ,VIPhotoViewDelegate>

@property (nonatomic, strong) UIScrollView *scrollView;

@property (nonatomic, strong) VIPhotoView *left;
@property (nonatomic, strong) VIPhotoView *currentView;
@property (nonatomic, strong) VIPhotoView *right;
@property (nonatomic, assign) kImageSkimType type;

@property (nonatomic, strong) UILabel *pageLabel;
@property(nonatomic, strong) UIPageControl *pageControl;

//@property (nonatomic, strong) UIScrollView *currentView;

@end

@implementation STImageSkimController

- (instancetype)initWithImages:(NSArray *)images andIndex:(NSInteger)index
{
    if (self = [super init]) {
        self.images = images;
        self.index = index;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    if (self.scrollView) {
        self.scrollView.contentOffset = CGPointMake(SCREEN_WIDTH, 0);
    }
    
//    [self setupPageLabel];
    [self setupPageControl];
}

- (void)setupPageLabel
{
    UILabel *pageLabel = [[UILabel alloc] init];
    pageLabel.frame = CGRectMake(412, 740, 100, 40);
    pageLabel.centerX = self.view.centerX;
    self.pageLabel = pageLabel;
    pageLabel.backgroundColor = [UIColor clearColor];
    pageLabel.textColor = [UIColor whiteColor];
    pageLabel.text = [NSString stringWithFormat:@"%d/%d", self.index + 1, self.images.count];
    [self.view addSubview:pageLabel];
    [self.view bringSubviewToFront:self.pageLabel];
}

- (void)setupPageControl{
    UIPageControl *pageControl = [[UIPageControl alloc] init];
    pageControl.frame = CGRectMake(412, 740, 180, 40);
    pageControl.centerX = self.view.centerX;
//    pageControl.backgroundColor = [UIColor redColor];
    pageControl.numberOfPages = self.images.count;
    self.pageControl = pageControl;
    [self.view addSubview:pageControl];
    [self.view bringSubviewToFront:self.pageControl];
}

- (UIScrollView *)scrollView
{
    if (_scrollView == nil) {
        _scrollView = [[UIScrollView alloc] initWithFrame:[UIScreen mainScreen].bounds];
        [_scrollView addSubview:self.currentView];
        [_scrollView addSubview:self.left];
        [_scrollView addSubview:self.right];
        _scrollView.contentSize = CGSizeMake(3 * SCREEN_WIDTH, 0);
        _scrollView.pagingEnabled = YES;
        _scrollView.delegate = self;
        _scrollView.bounces = NO;
        _scrollView.backgroundColor = [UIColor clearColor];
        
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.showsVerticalScrollIndicator = NO;
        [self.view addSubview:_scrollView];
    }
    return _scrollView;
}

- (VIPhotoView *)left
{
    if (_left == nil) {
        _left = [[VIPhotoView alloc] initWithFrame:_scrollView.frame];
        _left.contentMode = UIViewContentModeScaleAspectFit;
        NSURL *url = [_images st_objectWithIndex:(_index - 1)];
        _left.backgroundColor = [UIColor blackColor];
//        [self setImageWithImageView:_left andCenter:CGPointMake(0.5*SCREEN_WIDTH, 0.5*SCREEN_HEIGHT) andURL:url];
        _left.imageURL = url;
    }
    
    
    return _left;
}

- (VIPhotoView *)currentView
{
    if (_currentView == nil) {
        _currentView = [[VIPhotoView alloc] initWithFrame:CGRectMake(_scrollView.frame.size.width, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        _currentView.backgroundColor = [UIColor blackColor];
        //        [_currentView addSubview:self.current];
        
        _currentView.userInteractionEnabled = YES;
        _currentView.imageURL = _images[_index];
        _currentView.photoViewdelegate = self;
    }
    return _currentView;
}

- (VIPhotoView *)right {
    if (_right == nil) {
        _right = [[VIPhotoView alloc] initWithFrame:CGRectMake(_scrollView.frame.size.width * 2, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        _right.contentMode = UIViewContentModeScaleAspectFit;
//        [self setImageWithImageView:_right andCenter:CGPointMake(2.5*SCREEN_WIDTH, 0.5*SCREEN_HEIGHT) andURL:_images[_index + 1]];
        _right.backgroundColor = [UIColor blackColor];
        NSURL *url = [_images st_objectWithIndex:(_index + 1)];
        _right.imageURL = url;
    }
    return _right;
}

#pragma mark - scroll view delegate

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if ([scrollView isEqual:_scrollView]) {
        // 前移
        if (scrollView.contentOffset.x == 2 * SCREEN_WIDTH) {
            [self refreshImageIsNext:YES];
        } else if (scrollView.contentOffset.x == 0) {
            [self refreshImageIsNext:NO];
        }
    }
}

// 刷新图片. bool选yes是下一个, 选no是上一个
- (void)refreshImageIsNext:(BOOL)isNext
{
    // 回到当前界面.回归正常大小
    _scrollView.contentOffset = CGPointMake(SCREEN_WIDTH, 0);
    _currentView.transform = CGAffineTransformIdentity;
    
    // 到底往哪去
    isNext ? self.index++ : self.index--;
}

- (void)setIndex:(NSInteger)index
{
    _index = index;
    
    // 设置图片
    [_currentView setImageURL:_images[index]];
    _left.imageURL = [_images st_objectWithIndex:(index - 1)];
    _right.imageURL = [_images st_objectWithIndex:(index + 1)];
    
    // 设置位置类型
    
    if (_images.count == 1) {
        self.type = kImageSkimTypeStartAndFinal;
        return;
    }
    
    if (_index == 0) {
        // 第一位
        self.type = kImageSkimTypeStart;
    } else if (_index == _images.count -1) {
        // 最后位
        self.type = kImageSkimTypeFinal;
    } else {
        // 中间任意位
        self.type = kImageSkimTypeProgress;
    }
//    [self.view bringSubviewToFront:self.pageLabel];
    [self.view bringSubviewToFront:self.pageControl];
//    self.pageLabel.text = [NSString stringWithFormat:@"%d/%d", index + 1, self.images.count];
    self.pageControl.numberOfPages = self.images.count;
    self.pageControl.currentPage = index;
}



- (void)setType:(kImageSkimType)type
{
    if (_type == type) {
        return;
    }
    
    _type = type;
    // 设置内边距
    if (self.scrollView) {
        switch (type) {
            case kImageSkimTypeStart:
                _scrollView.contentInset = UIEdgeInsetsMake(0, -SCREEN_WIDTH, 0, 0);
                break;
            case kImageSkimTypeProgress:
                _scrollView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
                break;
            case kImageSkimTypeFinal:
                _scrollView.contentInset = UIEdgeInsetsMake(0, 0, 0, -SCREEN_WIDTH);
                break;
            case kImageSkimTypeStartAndFinal:
                _scrollView.contentInset = UIEdgeInsetsMake(0, -SCREEN_WIDTH, 0, -SCREEN_WIDTH);
                break;
            default:
                break;
        }
    }
}


- (void)setImageWithImageView:(UIImageView *)imageView andCenter:(CGPoint)center andURL:(NSURL *)url
{
    [imageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"img_unselect"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        imageView.size = [image suitableSize];
        imageView.center = center;
    }];
}

- (void)tapOneTime
{
    [self dismissViewControllerAnimated:NO completion:^{
        if (self.returnIndex) {
            self.returnIndex(self.index);
        }
    }];
    
}

- (void)sendReturnIndexBlock:(ReturnIndex)returnIndex{
    self.returnIndex = returnIndex;
}

@end
